<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>定时器</title>
  </head>
  <body>
    <h2 id="h2"></h2>
    <script>
      var h2 = document.getElementById("h2");
      var num = 1;
      var timer = setInterval(function () {
        h2.innerHTML = num++;
        if (num === 11) {
          clearInterval(timer);
        }
      }, 1000);
    </script>
  </body>
</html>
